package io.gitee.tooleek.lock.spring.boot.core.key;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.gitee.tooleek.lock.spring.boot.constant.LockCommonConstant;
import io.gitee.tooleek.lock.spring.boot.enumeration.InterProcess;

public class ZookeeperLockKey extends LockKey {

	private InterProcess interProcess;

	private long waitTime = -1;

	private TimeUnit timeUnit = TimeUnit.SECONDS;

	private ZookeeperLockKey(List<String> keyList, InterProcess interProcess, long waitTime, TimeUnit timeUnit) {
		super(keyList);
		this.interProcess = interProcess;
		this.waitTime = waitTime;
		this.timeUnit = timeUnit;
	}

	public InterProcess getInterProcess() {
		return this.interProcess;
	}

	public long getWaitTime() {
		return this.waitTime;
	}

	public TimeUnit getTimeUnit() {
		return this.timeUnit;
	}

	public List<String> getLockPath() {
		List<String> pathList = new ArrayList<String>();
		for (String key : this.getKeyList()) {
			pathList.add(LockCommonConstant.OBLIQUE
					+ key.replace(LockCommonConstant.KEY_SPLIT_MARK, LockCommonConstant.OBLIQUE));
		}
		return pathList;
	}

	public static ZookeeperKeyBuilder newZookeeperKeyBuilder() {
		return new ZookeeperKeyBuilder();
	}

	public static class ZookeeperKeyBuilder extends Builder {

		private InterProcess interProcess = InterProcess.MUTEX;
		private long waitTime = -1;
		private TimeUnit timeUnit = TimeUnit.SECONDS;

		public ZookeeperKeyBuilder interProcess(InterProcess interProcess) {
			this.interProcess = interProcess;
			return this;
		}

		public ZookeeperKeyBuilder waitTime(long waitTime) {
			this.waitTime = waitTime;
			return this;
		}

		public ZookeeperKeyBuilder timeUnit(TimeUnit timeUnit) {
			this.timeUnit = timeUnit;
			return this;
		}

		public ZookeeperLockKey build() {
			return new ZookeeperLockKey(keyList, interProcess, waitTime, timeUnit);
		}

	}

}
